package com.etoak.common.core.vo;

import com.etoak.common.core.enums.ResultEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * Rest 接口统一返回值
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResultVO<T> {

    private Integer code;

    private String message;

    private T data;

    public ResultVO(ResultEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.message = resultEnum.getMessage();
    }

    public ResultVO(ResultEnum resultEnum, T data) {
        this.code = resultEnum.getCode();
        this.message = resultEnum.getMessage();
        this.data = data;
    }

    /**
     * 添加、更新、删除接口执行成功后调用
     */
    public static <T> ResultVO<T> success() {
        return new ResultVO<>(ResultEnum.SUCCESS);
    }

    /**
     * 查询接口: 返回对象、返回数组、List 等
     */
    public static <T> ResultVO<T> success(T data) {
        return new ResultVO<>(ResultEnum.SUCCESS, data);
    }

    public static <T> ResultVO<T> failed() {
        return new ResultVO<>(ResultEnum.SYSTEM_ERROR);
    }

    public static <T> ResultVO<T> failed(ResultEnum resultEnum) {
        return failed(resultEnum.getCode(), resultEnum.getMessage());
    }

    public static <T> ResultVO<T> failed(int code, String message) {
        return new ResultVO<>(code, message, null);
    }

    public static <T> ResultVO<T> failed(String message) {
        return failed(ResultEnum.SYSTEM_ERROR.getCode(), message);
    }
}